package com.kungfuhacking.wristbandpro.my.activity;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.bean.Basebean;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.base.view.HomeActivity;
import com.kungfuhacking.wristbandpro.custom.InputView;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.CommonUtils;
import com.kungfuhacking.wristbandpro.utils.Constant;

import io.reactivex.disposables.Disposable;

/**
 * 修改密码
 * Created by Aaron on 17/5/15.
 */

public class ModifyPwdActivity extends BaseActivity {

    private InputView ivnewpsd;
    private InputView ivnewpsdreped;
    private InputView ivoldpsd;
    private Disposable disposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_modify_password);

        ivnewpsdreped = (InputView) findViewById(R.id.ivnewpsdreped);
        ivnewpsd = (InputView) findViewById(R.id.ivnewpsd);
        ivoldpsd = (InputView) findViewById(R.id.ivoldpsd);
        TitleBarView tbv = (TitleBarView) findViewById(R.id.tbv);

        ivnewpsdreped.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        ivnewpsd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        ivoldpsd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tbv.setOnRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyPassword();
            }
        });
    }

    /**
     * 修改密码
     */
    private void modifyPassword() {

        if (TextUtils.isEmpty(ivoldpsd.getText())) {
            showErr("请输入旧密码");
            return;
        }

        if (TextUtils.isEmpty(ivnewpsd.getText())) {
            showErr("请输入新密码");
            return;
        }

        if (ivnewpsd.getText().length() < 6 || ivnewpsd.getText().length() > 16) {
            showErr("密码长度在6-16位之间");
            return;
        }

        if (!ivnewpsd.getText().equals(ivnewpsdreped.getText())) {
            showErr("两次输入的密码不一致");
            return;
        }

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("oldpwd", CommonUtils.toMd5(ivoldpsd.getText().getBytes()).toUpperCase());
        j.put("newpwd", CommonUtils.toMd5(ivnewpsd.getText().getBytes()).toUpperCase());

        RetrofitHttpUtil.getInstance().updatePwd(new BaseNoTObserver<Basebean>() {
            @Override
            public void onHandleSuccess(Basebean basebean) {
                hideLoading();
                showErr("修改成功");
                sp.edit().putString(Constant.PASSWORD, CommonUtils.toMd5(ivnewpsd.getText().getBytes())).apply();
                nextActivity(HomeActivity.class);
            }

            @Override
            public void onHandleError(String message) {
                showErr(message);
                hideLoading();
            }

            @Override
            public void onReLogin(String message) {
                hideLoading();
                showErr(message);
                nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                disposable = d;
                showLoading("修改中...");
            }
        }, new RequestBean().setData(j.toString()));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (disposable != null)
            disposable.dispose();
    }
}
